package company.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import company.common.R;
import company.domain.Petsimgs;
import company.domain.Rescuestations;
import company.domain.Straypets;
import company.service.PetsimgsService;
import company.service.StraypetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author liuzewei
 * @ description
 * @ date 2024/7/2
 */
@RestController
public class CatController {

    @Autowired
    StraypetsService straypetsService;
    @Resource
    PetsimgsService petsimgsService;


    /**
     * 流浪宠物分页查询
     */
    @PostMapping("/selectstraypets/{page}")
    public R SelectAddress(@PathVariable("page") int page){
        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,5);
        List<Straypets> list=this.straypetsService.list();
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }

    /*
     * 根据id查询流浪宠物信息
     * */
    @PostMapping("/updatestraypetsid")
    public R SelectAddressId(@RequestBody Straypets straypets){
        Straypets byId = this.straypetsService.getById(straypets.getStraypetid());
        return new R().add("byId",byId);
    }

    /**
     * 修改流浪宠物信息
     */
    @PostMapping("/updatestraypets")
    public R Update(@RequestBody Straypets straypets){
        boolean b = this.straypetsService.updateById(straypets);
        if (b){
            new R().setCode(502);
            System.out.println(new R().getCode());
        }
        return new R();
    }


    /*
     * 流浪宠物信息删除
     * */
    @RequestMapping("/delrescuestraypets/{straypetid}")
    public R delrescuestraypets(@PathVariable("straypetid") int id){
        boolean remove = this.straypetsService.removeById(id);
        R r=new R();
        if(remove){
            r.setCode(502);
        }
        return r;
    }


    /*
     * 新增流浪宠物
     * */
    @PostMapping("/Addstraypets")
    public R Addstraypets(@RequestBody Straypets straypets){
        boolean save = this.straypetsService.save(straypets);
        if (save){
            new R().setCode(502);
        }
        return new R();
    }

    /*
    * 流浪动物救助
    * */
    @RequestMapping("/selectstraypets2")
    public R selectstraypets2(){
        List<Map> list = this.straypetsService.selectStraypets();
        List<Petsimgs> list1 = this.petsimgsService.list();
        return new R().add("list",list).add("list1",list1);
    }

}
